<?php

namespace app\controllers;

use Yii;
use YiiBase;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;
use app\models\UploadForm;
use yii\web\UploadedFile;




class UploadController extends SecBaseController
{
    
    public function actionUeditorctrl(){

       // echo 'hehe';exit; 
        $response = Yii::$app->response;
        $request = Yii::$app->request;
        $action = $request->get('action') ;
        if($action=="config"){
            
            $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(dirname(__FILE__)."/config1.json")), true);
            $result =  json_encode($CONFIG);
            echo $result ;exit ; 
        } else{
            return $this->actionImg();
        }

    }



    public function actionImg(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $ret = array();
        $model = new UploadForm();
        $res = array() ; 
        $img_url = false ; 
        try{
           if (Yii::$app->request->isPost) {
                $model->image_file = UploadedFile::getInstanceByName('image_file');
                if($model->image_file!=null){
                    $img_url = $model->upload() ;
                   
                    if (!!$img_url) {
                         if("uploadimage"==$request->get("action")){
                            $ret["state"] = 'SUCCESS';
                            $ret["url"] =  $img_url;
                            return $ret ;
                         }else{
                             $ret["errno"] = 0;
                            $ret["url"] =  $img_url;
                            return $ret ; 
                         }
                       
                    }
                }


                $model->image_file = UploadedFile::getInstanceByName('imgFile');
                if($model->image_file!=null){
                    $img_url = $model->upload() ;
                    if (!!$img_url) {
                        $ret["error"] = 0;
                        $ret["url"] =  $img_url;
                        return $ret ; 
                    }
                }


                $model->image_file = UploadedFile::getInstanceByName('upfile');
                if($model->image_file!=null){
                    if (!!$img_url) {
                        $ret["state"] = 'SUCCESS';
                        $ret["url"] =  $img_url;
                        return $ret ; 
                    }
                }


                $ret["errno"] = -1;
                $ret["errmsg"] = "没有找到上传图片的字段，尝试了image_file和imgFile和upfile";
                return $ret;

                
            }
        }catch(\Exception $e){
            $ret["errno"] = -1;
            $ret["errmsg"] = $e->getMessage(); 
        }

        return $ret;
    }

    public function actionTest(){

             
    }


    
}
